﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryManagement
{
    public static class clsUpdate
    {
        private static LibManagementEntities context;
        private static String comments;
        private static List<String> listStpUpdates;
        private static List<String> listUpdateVersions = new List<string> 
        {
            "v1001", "v1002"
        };
        private static stpUpdate OStpUpdate;
        private static string query;
        public static void UpdateDatabase()
        {
            try
            {
                context = new LibManagementEntities();
                listStpUpdates = context.stpUpdates.Select(x => x.UpdateVersion).ToList();
                foreach (var update in listUpdateVersions)
                {
                    if (!listStpUpdates.Contains(update))
                    {
                        StartUpdate(update);
                        context.SaveChanges();
                        InsertIntoStpUpdate(update, comments);
                        comments = null;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private static void InsertIntoStpUpdate(string update, string comments)
        {
            OStpUpdate = new stpUpdate();
            OStpUpdate.Date = DateTime.Today;
            OStpUpdate.UpdateVersion = update;
            OStpUpdate.Comments = comments;
            context.stpUpdates.Add(OStpUpdate);
            context.SaveChanges();
        }

        private static void StartUpdate(string update)
        {

            switch (update)
            {
                case "v1001":
                    updateV1001();
                    comments = "First update of the LibManagement.";
                    break;

                case "v1002":
                    updateV1002();
                    comments = "Added new columns in Category Table";
                    break;

                case "v1003":
                    break;

                case "v1004":
                    break;

                case "v1005":
                    break;

                case "v1006":
                    break;

                case "v1007":
                    break;

                default:
                    break;
            }

        }

        private static void updateV1002()
        {
            query = @"BEGIN TRANSACTION
                    SET QUOTED_IDENTIFIER ON
                    SET ARITHABORT ON
                    SET NUMERIC_ROUNDABORT OFF
                    SET CONCAT_NULL_YIELDS_NULL ON
                    SET ANSI_NULLS ON
                    SET ANSI_PADDING ON
                    SET ANSI_WARNINGS ON
                    COMMIT
                    BEGIN TRANSACTION
                    ALTER TABLE dbo.Category ADD
	                    DueDate numeric(18, 0) NULL,
	                    FinePerDay numeric(18, 0) NULL
                    ALTER TABLE dbo.Category SET (LOCK_ESCALATION = TABLE)
                    COMMIT";
            context.Database.ExecuteSqlCommand(query);
        }

        private static void updateV1001()
        {

        }
    }
}
